#include "stdafx.h"
#include "BenzenBond.h"
#include "math.h"


CBenzenBond::CBenzenBond(void)
{
}

CBenzenBond::CBenzenBond(CPoint pBegin, CPoint pEnd, COLORREF m_cPen)
{
	this->m_pBegin  = pBegin;
	this->m_pEnd	= pEnd;
	m_nAngle = NUM_OF_ANGLE;
	m_pCoordinates = new CPoint[NUM_OF_ANGLE];
	this->m_cPenColor = m_cPen;
	m_brushColor.CreateSolidBrush(RGB(255, 255, 255));
	m_nPenStyle = PS_SOLID;
	m_nPenWidth = 1;
}

CBenzenBond::CBenzenBond (CPoint m_pBegin, CPoint m_pEnd, COLORREF m_clrBack, COLORREF m_cPenColor, BASESHAPE__T m_nShapeType)
{
	this->m_pBegin = m_pBegin;
	this->m_pEnd = m_pEnd;
	this->m_clrBack = m_clrBack;
	this->m_cPenColor = m_cPenColor;
	this->m_nShapeType = m_nShapeType;
//	this->m_szText = m_szText;
//	this->m_color = m_color;
	m_nAngle = NUM_OF_ANGLE;
	m_pCoordinates = new CPoint[NUM_OF_ANGLE];
	m_nPenStyle = PS_SOLID;
	m_nPenWidth = 1;
}

void CBenzenBond::Serialize (CArchive &ar)
{
	if (ar.IsStoring() )
	{
		ar << m_pBegin << m_pEnd << m_clrBack << m_cPenColor << m_nShapeType << m_szText << m_color;
	}
	else
	{
		int nShapeType;
		ar >> m_pBegin >> m_pEnd >> m_clrBack >> m_cPenColor >> nShapeType >> m_szText >> m_color;
		m_nShapeType = (BASESHAPE__T)nShapeType;
	}
}


BOOL CBenzenBond::Draw(CDC *cDC)
{
	int lpPts[] = { 2, 2, 2};
	radiansOffset = DegreesToRadians(AngleDist());
	CPen cPen(m_nPenStyle, m_nPenWidth, m_cPenColor);
	cDC->SelectObject(&m_brushColor);
	CPen *pOldPen = cDC->SelectObject(&cPen);
	this->setCoordinate();
	cDC->Polygon(m_pCoordinates, 6);
	cDC->PolyPolygon(m_pCoordinates + 6, lpPts, 3);
	cDC->SelectObject(pOldPen);
	pOldPen->DeleteObject();
	return	FALSE;
}

void CBenzenBond::setCoordinate()
{
	//const double radius = Radius;
	CPoint m_pTemp(15, 15);
	CPoint Center;
	CPoint Center2;
	CPoint Center3;
	CPoint Center4;
	m_pCoordinates[0] =  m_pMax = m_pMin = m_pBegin;
	//main center point
	Center.x = m_pBegin.x - 30 * cos(radiansOffset + 0 * 2 * PI / 6);
	Center.y = m_pBegin.y - 30 * sin(radiansOffset + 0 * 2 * PI / 6);
	// center point 2
	Center2.x = m_pBegin.x - 30 * cos(radiansOffset + 0 * 2 * PI / 6);
	Center2.y = m_pBegin.y - 30 * sin(radiansOffset + 0 * 2 * PI / 6);
	//center point 3
	Center3.x = m_pBegin.x - 30 * cos(radiansOffset + 0 * 2 * PI / 6);
	Center3.y = m_pBegin.y - 30 * sin(radiansOffset + 0 * 2 * PI / 6);
	//center point 4
	Center4.x = m_pBegin.x - 30 * cos(radiansOffset + 0 * 2 * PI / 6);
	Center4.y = m_pBegin.y - 30 * sin(radiansOffset + 0 * 2 * PI / 6);
	for( int i=1; i < 6; ++i )
	{
		const double angle = radiansOffset + i * 2 * PI /6;
		m_pCoordinates[i].x = Center.x + 30 * cos(angle);
		m_pCoordinates[i].y = Center.y + 30 * sin(angle);
	}
	//angle one
	double angle = radiansOffset + 1 * 2 * PI /6;
	m_pCoordinates[6].x = Center2.x + 22 * cos(angle);
	m_pCoordinates[6].y = Center2.y + 22 * sin(angle);

	angle = radiansOffset + 2 * 2 * PI /6;
	m_pCoordinates[7].x = Center2.x + 22 * cos(angle);
	m_pCoordinates[7].y = Center2.y + 22 * sin(angle);
	//angle two
	angle = radiansOffset + 3 * 2 * PI /6;
	m_pCoordinates[8].x = Center2.x + 22 * cos(angle);
	m_pCoordinates[8].y = Center2.y + 22 * sin(angle);

	angle = radiansOffset + 4 * 2 * PI /6;
	m_pCoordinates[9].x = Center3.x + 22 * cos(angle);
	m_pCoordinates[9].y = Center3.y + 22 * sin(angle);
	//angle three
	angle = radiansOffset + 5 * 2 * PI /6;
	m_pCoordinates[10].x = Center2.x + 22 * cos(angle);
	m_pCoordinates[10].y = Center2.y + 22 * sin(angle);

	angle = radiansOffset + 6 * 2 * PI /6;
	m_pCoordinates[11].x = Center3.x + 22 * cos(angle);
	m_pCoordinates[11].y = Center3.y + 22 * sin(angle);
	
	for(int i = 0; i < m_nAngle; i++)
	{
		m_pMin.x = min(m_pMin.x, m_pCoordinates[i].x);
		m_pMin.y = min(m_pMin.y, m_pCoordinates[i].y);
		m_pMax.x = max(m_pMax.x, m_pCoordinates[i].x);
		m_pMax.y = max(m_pMax.y, m_pCoordinates[i].y);

	}
	
}

int CBenzenBond::AngleDist()
{
	//he so goc cua duong thang
	INT A, B; 
	//goc cua duong thang
	float Angle;
	//tinh he so cua duong thang
	A = m_pEnd.y - m_pBegin.y;
	B = m_pBegin.x - m_pEnd.x;
	//tinh goc
	Angle = B / sqrtf((float)A * A + B * B);
	if(m_pEnd.y > m_pBegin.y)
		return 360 - acos(Angle) * 180.0 / PI;
	return acos(Angle) * 180.0 / PI;
}

void CBenzenBond::DrawNullRect(CDC *cDC)
{
	const unsigned int Width = 4;
	CRect m_rectT(CPoint(m_pBegin.x - Width, m_pBegin.y - Width), CPoint(m_pBegin.x + Width, m_pBegin.y + Width));
	CPen cPen(m_nPenStyle, 1, m_cPenColor);
	cDC->SelectObject(&cPen);
	cDC->Rectangle(CRect(CPoint(m_pBegin.x - Width, m_pBegin.y - Width), CPoint(m_pBegin.x + Width, m_pBegin.y + Width)));
	cDC->Rectangle(CRect(CPoint(m_pEnd.x - Width, m_pEnd.y - Width), CPoint(m_pEnd.x + Width, m_pEnd.y + Width)));

}

CBenzenBond::~CBenzenBond()
{
}